package com.fsc.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.json.ObjectMapper;
import com.fsc.entity.ResultWrapper;
import com.fsc.entity.domain.ChildSituation;
import com.fsc.service.ChildSituationService;
import com.fsc.service.LoginInfoService;
import com.fsc.service.StuPartTeachService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.ZoneId;
import java.util.List;

import static com.fsc._enum.ReturnEnum.SUCCESS;

@RestController
@RequestMapping("/monitoring")
public class ChildSituationController {

    @Autowired
    private ChildSituationService childSituationService;

    @Autowired
    private StuPartTeachService stuPartTeachService;

    @Autowired
    private LoginInfoService loginInfoService;

    @GetMapping("/videos")
    public ResultWrapper<List<ChildSituation>> getVideosByClassId(@RequestParam Integer classId) {
        return ResultWrapper.success(SUCCESS, childSituationService.getVideosByClassId(classId));
    }

    @PostMapping("/upload")
    public ResultWrapper<String> uploadVideo(@RequestParam("file") MultipartFile file,
                                             @RequestParam("classId") Integer classId,
                                             @RequestParam("time") @DateTimeFormat(pattern = "yyyy-MM-dd") String time) {
        try {
            childSituationService.uploadVideo(classId, file, time);
            return ResultWrapper.success(SUCCESS, "视频上传成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResultWrapper.fail(SUCCESS, "视频上传失败");
        }
    }

    @GetMapping("/classes")
    public ResultWrapper<List<Integer>> getRelatedClasses() {
        Integer loginId = Integer.parseInt((String) StpUtil.getLoginId());
        int role = loginInfoService.getRole(loginId);

        List<Integer> classIds = null;

        if (role == 0) { // 领导
            classIds = stuPartTeachService.getAllClassIds();
        } else if (role == 1) { // 老师
            classIds = stuPartTeachService.getClassIdByTeacherId(loginId);
        } else if (role == 2) { // 家长
            classIds = stuPartTeachService.getClassIdByParentId(loginId);
        } else if (role == 3) { // 学生
            classIds = stuPartTeachService.getClassIdByStudentId(loginId);
        }

        return ResultWrapper.success(SUCCESS, classIds);
    }

    @PostMapping("/add")
    public ResultWrapper<String> addChildSituation(@RequestBody ChildSituation childSituation) {
        childSituationService.addChildSituation(childSituation);
        return ResultWrapper.success(SUCCESS, "视频监控信息添加成功");
    }

    @DeleteMapping("/delete/{id}")
    public ResultWrapper<String> deleteChildSituation(@PathVariable Integer id) {
        childSituationService.deleteChildSituation(id);
        return ResultWrapper.success(SUCCESS, "视频监控信息删除成功");
    }

    @PutMapping("/update")
    public ResultWrapper<String> updateVideo(@RequestParam("id") Integer id,
                                             @RequestParam(value = "file", required = false) MultipartFile file,
                                             @RequestParam(value = "classId", required = false) Integer classId,
                                             @RequestParam(value = "time", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") String time) {
        try {
            childSituationService.updateVideo(id, classId, file, time);
            return ResultWrapper.success(SUCCESS, "视频更新成功");
        } catch (IOException e) {
            e.printStackTrace();
            return ResultWrapper.fail(SUCCESS, "视频更新失败");
        }
    }

}
